package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.Resmsg;
import com.fhnew.app.model.network.entity.req.Respass;
import com.fhnew.app.util.Md5;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ForgetActivity extends Activity {

    ImageView close;
    TextView titletext;
    EditText changephone;
    EditText changepasset;
    EditText newchangepass;
    EditText rigistusername;
    TextView code_img;
    TextView changebt;
    LinearLayout passlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        Constants.backgroundvoic = false;
        close = findViewById(R.id.close);
        titletext = findViewById(R.id.titletext);
        changephone = findViewById(R.id.changephone);
        changepasset = findViewById(R.id.changepasset);
        newchangepass = findViewById(R.id.newchangepass);
        rigistusername = findViewById(R.id.rigistusername);
        code_img = findViewById(R.id.code_img);
        changebt = findViewById(R.id.changebt);

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        code_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(changephone.getText().toString().trim())) {
                    sendmsg(changephone.getText().toString().trim());
                    timer.start();
                } else {
                    Toast.makeText(ForgetActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }
            }
        });
        changebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(changephone.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(changepasset.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(newchangepass.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(rigistusername.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                changepassword(changephone.getText().toString().trim(), rigistusername.getText().toString().trim(),
                        Md5.toMd5(changepasset.getText().toString().trim()), Md5.toMd5(newchangepass.getText().toString().trim()));
            }
        });

    }

    private void changepassword(String trim, String trim1, String s, String s1) {
        HttpJsonUtils.getHttpApiInstance()
                .changepass(Constants.getuseAdress() + "api/m/forgotPassword", trim, trim1, s, s1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Respass>() {
                    @Override
                    protected void onCustomNext(Respass resLogin) {
                        if (resLogin.status) {
                            timer.cancel();
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        Toast.makeText(ForgetActivity.this, "输入无效", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            code_img.setText((millisUntilFinished / 1000) + "S后重发");
            code_img.setEnabled(false);
        }

        @Override
        public void onFinish() {
            code_img.setEnabled(true);
            code_img.setText("获取验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    /**
     * 发送验证码
     *
     * @param trim
     */
    private void sendmsg(String trim) {
        HttpJsonUtils.getHttpApiInstance()
                .sendmsg(Constants.getuseAdress() + "api/m/smsCode/" + trim, "1", "login")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resmsg>() {
                    @Override
                    protected void onCustomNext(Resmsg resLogin) {
                        if (resLogin.status) {
                            //成功
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                            timer.cancel();
                            code_img.setEnabled(true);
                            code_img.setText("获取验证码");
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        timer.cancel();
                    }
                });
    }

}
